package com.polaris.live.utils

import android.content.Intent
import com.polaris.live.common.util.ifThen

/**
 * IntentUtils
 *
 * @author Created by lucas on 2023/10/24 15:34
 */

/**
 * 获取Boolean类型的值
 */
fun Intent.getBooleanOrString(key: String, defVal: Boolean): Boolean {
    return hasExtra(key).ifThen {
        getStringExtra(key)?.let {
            it.equals("true", ignoreCase = true) || it == "1"
        } ?: getBooleanExtra(key, defVal)
    } ?: defVal
}

/**
 * 获取Byte类型的值
 */
fun Intent.getByteOrString(key: String, defVal: Byte): Byte {
    return hasExtra(key).ifThen {
        getStringExtra(key)?.toByteOrNull() ?: getByteExtra(key, defVal)
    } ?: defVal
}

/**
 * 获取Short类型的值
 */
fun Intent.getShortOrString(key: String, defVal: Short): Short {
    return hasExtra(key).ifThen {
        getStringExtra(key)?.toShortOrNull() ?: getShortExtra(key, defVal)
    } ?: defVal
}

/**
 * 获取Int类型的值
 */
fun Intent.getIntOrString(key: String, defVal: Int): Int {
    return hasExtra(key).ifThen {
        getStringExtra(key)?.toIntOrNull() ?: getIntExtra(key, defVal)
    } ?: defVal
}

/**
 * 获取Long类型的值
 */
fun Intent.getLongOrString(key: String, defVal: Long): Long {
    return hasExtra(key).ifThen {
        getStringExtra(key)?.toLongOrNull() ?: getLongExtra(key, defVal)
    } ?: defVal
}

/**
 * 获取Float类型的值
 */
fun Intent.getFloatOrString(key: String, defVal: Float): Float {
    return hasExtra(key).ifThen {
        getStringExtra(key)?.toFloatOrNull() ?: getFloatExtra(key, defVal)
    } ?: defVal
}

/**
 * 获取Double类型的值
 */
fun Intent.getDoubleOrString(key: String, defVal: Double): Double {
    return hasExtra(key).ifThen {
        getStringExtra(key)?.toDoubleOrNull() ?: getDoubleExtra(key, defVal)
    } ?: defVal
}
